﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using SilverlightClient.ServiceReferenceLogin;

namespace SilverlightClient
{
    public partial class Login : Page
    {
        ServiceReferenceLogin.LoginServicesClient service = new ServiceReferenceLogin.LoginServicesClient();

        public Login()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            lblErrores.Visibility = Visibility.Collapsed;
            service.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(LoginCompleted_EventHandler);
            try
            {
                string usuario = txtUsuario.Text;
                string password = txtPassword.Password;
                if (string.IsNullOrEmpty(usuario) || string.IsNullOrEmpty(password))
                {
                    lblErrores.Visibility = Visibility.Visible;
                    lblErrores.Content = "Se deben llenar ambos campos para iniciar sesión.";
                }
                else
                {
                    service.LoginAsync(usuario, password);
                }
            }
            catch (Exception ex)
            {
                lblErrores.Visibility = Visibility.Visible;
                lblErrores.Content = ex.Message;
            }
        }

        internal void LoginCompleted_EventHandler(object sender, LoginCompletedEventArgs e)
        {
            try
            {
                Usuario usuario = e.Result;                

                if (usuario != null)
                {
                    Grid root = Application.Current.RootVisual as Grid;
                    if (root != null)
                    {
                        if (Utilities.EsAdminSeguidor(usuario) || Utilities.EsSeguidor(usuario))
                        {
                            root.Children.RemoveAt(0);
                            root.Children.Add(new MainPage(usuario));
                        }
                        else
                        {
                            if (Utilities.EsAdmin(usuario))
                            {
                                root.Children.RemoveAt(0);
                                root.Children.Add(new AdministradorVideos(usuario));
                            }
                            else
                            {
                                lblErrores.Visibility = Visibility.Visible;
                                lblErrores.Content = "El usuario no tiene rol asignado";
                            }
                        }
                    }
                }
                else
                {
                    txtUsuario.Text = string.Empty;
                    txtPassword.Password = string.Empty;
                    lblErrores.Visibility = Visibility.Visible;
                    lblErrores.Content = "Usuario o Password Invalidos";
                }
            }
            catch (Exception ex)
            {
                txtUsuario.Text = string.Empty;
                txtPassword.Password = string.Empty;
                lblErrores.Visibility = Visibility.Visible;
                lblErrores.Content = ex.Message;
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                btnLogin_Click(sender, e);
        }

    }
}
